const { Article, User } = require('../model');
// 从关注的用户那里获取最新的文章
module.exports.getFollowArticles = async (req, res, next) => {
    try {
        res.send('get /feed')
    } catch (err) {
        next(err)
    }
}
// 获取文章列表
module.exports.getArticles = async (req, res, next) => {
    try {
        const {
            limit = 20,
            offset = 0,
            tag,
            author
        } = req.query
        const filter = {}
        if (tag) {
            filter.tagList = tag
        }
        if (author) {
            const user = await User.findOne({ username: author })
            filter.author = user ? user._id : null
        }
        const articles = await Article.find(filter).sort({
            // -1 倒叙，1-升序
            createdAt: -1
        })
            .skip(Number.parseInt((offset - 1) * limit))
            .limit(Number.parseInt(limit))
        const articleCount = await Article.countDocuments()
        res.status(200).json({
            articles,
            articleCount
        })
    } catch (err) {
        next(err)
    }
}
// 创建文章
module.exports.createArticle = async (req, res, next) => {
    try {
        const article = new Article(req.body.article)
        article.author = req.user._id
        await article.populate('author').execPopulate()
        await article.save()
        res.status(201).json({
            article
        })
    } catch (err) {
        next(err)
    }
}
// 获取一篇文章
module.exports.getArticle = async (req, res, next) => {
    try {
        const article = await Article.findById(req.params.articleId).populate('author')
        if (!article) {
            return res.status(404).end('没有对应的文章')
        }
        res.status(200).json({
            article
        })
    } catch (err) {
        next(err)
    }
}
// 修改文章
module.exports.updateArticle = async (req, res, next) => {
    try {
        const article = req.article
        const bodyArticle = req.body.article
        article.title = bodyArticle.title || article.title
        article.description = bodyArticle.description || article.description
        article.body = bodyArticle.body || article.body
        await article.save()
        res.status(200).json({
            article
        })
    } catch (err) {
        next(err)
    }
}
// 删除文章
module.exports.deleteArticle = async (req, res, next) => {
    try {
        const article = req.article
        await article.remove()
        res.status(200).end('删除成功')
    } catch (err) {
        next(err)
    }
}
// 获取评论列表
module.exports.getComments = async (req, res, next) => {
    try {
        res.send('get /:slug/comments')
    } catch (err) {
        next(err)
    }
}
// 发表评论
module.exports.sendComment = async (req, res, next) => {
    try {
        res.send('get /:slug/comments')
    } catch (err) {
        next(err)
    }
}
// 删除评论
module.exports.deleteComment = async (req, res, next) => {
    try {
        res.send('get /:slug/comments/:id')
    } catch (err) {
        next(err)
    }
}
// 收藏文章
module.exports.favoriteComment = async (req, res, next) => {
    try {
        res.send('post /:slug/favorite')
    } catch (err) {
        next(err)
    }
}
// 取消收藏
module.exports.cancelFavoriteComment = async (req, res, next) => {
    try {
        res.send('delete /:slug/favorite')
    } catch (err) {
        next(err)
    }
}